package com.gqj.system.hk;

import NetSDKDemo.HCNetSDK;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.gqj.common.core.domain.entity.SysUser;
import com.gqj.common.utils.SecurityUtils;
import com.gqj.elec.service.WebSocketServer;
import com.gqj.system.service.ISysUserService;
import com.sun.jna.Pointer;
import org.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.HashMap;
import java.util.Map;

@Component
public class UserAlarmCallback implements HCNetSDK.FMSGCallBack_V31 {
    @Autowired
    private ISysUserService userService;

    @Override
    public boolean invoke(int lCommand, HCNetSDK.NET_DVR_ALARMER pAlarmer, Pointer pAlarmInfo, int dwBufLen, Pointer pUser) {
        System.out.println("报警事件类型： lCommand:" + Integer.toHexString(lCommand));

        switch (lCommand) {
            case HCNetSDK.COMM_ALARM_ACS: //门禁主机报警信息
                HCNetSDK.NET_DVR_ACS_ALARM_INFO strACSInfo = new HCNetSDK.NET_DVR_ACS_ALARM_INFO();
                strACSInfo.write();
                Pointer pACSInfo = strACSInfo.getPointer();
                pACSInfo.write(0, pAlarmInfo.getByteArray(0, strACSInfo.size()), 0, strACSInfo.size());
                strACSInfo.read();
                /**门禁事件的详细信息解析，通过主次类型的可以判断当前的具体门禁类型，例如（主类型：0X5 次类型：0x4b 表示人脸认证通过，
                 主类型：0X5 次类型：0x4c 表示人脸认证失败）*/
                if (Integer.toHexString(strACSInfo.dwMajor).equals("5") && Integer.toHexString(strACSInfo.dwMinor).equals("4b")) {
                    System.out.println("刷脸 工号1：" + strACSInfo.struAcsEventInfo.dwEmployeeNo);
                    SysUser sysUser = userService.selectUserByUserName(strACSInfo.struAcsEventInfo.dwEmployeeNo + "");
                    if(sysUser!=null){
                        // 创建ObjectMapper对象
                        Map<String,String> response = new HashMap<>();
                        response.put("key","auto_login");
                        response.put("userName",sysUser.getUserName());
                        response.put("password",sysUser.getRealPassword());

                        ObjectMapper objectMapper = new ObjectMapper();

                        try {
                            // 将对象转换为JSON字符串
                            String jsonString = objectMapper.writeValueAsString(response);
                            System.out.println("刷脸socket:"+jsonString);
                            WebSocketServer.sendInfo(jsonString);
                            System.out.println(jsonString);
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }else{
                        System.out.println("未查到对应的用户信息");
                    }

                }
                break;
        }
        return true;
    }
}
